package com.tqyan.service.chat.impl.paper;

import com.tqyan.dal.mapper.PaperMapper;
import com.tqyan.model.dto.*;
import com.tqyan.model.enums.PaperPayStatusEnum;
import com.tqyan.model.exp.DefaultException;
import com.tqyan.service.chat.AbstractChatPointService;
import com.tqyan.service.chat.ChatPointContent;
import com.tqyan.service.service.PaperService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 聊天指令: 取消
 * <p> <功能详细描述> </p>
 *
 * @author 陈晨
 * @version 1.0
 * @date 2023/5/4
 */
@Service
@Slf4j
public class PaperCancelChatPointService extends AbstractChatPointService {

    @Autowired
    private PaperMapper paperMapper;
    @Autowired
    private PaperService paperService;

    @Override
    public String getPoint() {
        return ChatPointContent.PAPER_CANCEL;
    }

    @Override
    public ChatContentDTO chatPoint(UserDTO user, UserSettingDTO userSetting, ChatDTO chat, String content) {
        // 查询用户未完成的论文
        PaperDTO paper = paperMapper.queryIncomplete(user.getId());
        if (null == paper) {
            return null;
        }
        // 判断支付状态
        String answerContent;
        if (PaperPayStatusEnum.PAID.getValue() == paper.getPayStatus()) {
            answerContent = "论文《" + paper.getTitle() + "》已支付、撰写中, 不可取消";
        } else {
            // 取消论文
            boolean result = paperService.cancel(user.getId(), paper.getId());
            log.info("[{}] userId={}, paper={}, 取消论文: {}", this.getPoint(), user.getId(), paper.getTitle(), result);
            if (!result) {
                throw new DefaultException("论文取消失败");
            }
            answerContent = "论文《" + paper.getTitle() + "》已取消";
        }
        // 记录对话
        ChatContentDTO question = super.createQuestion(user, userSetting, chat, content, null, answerContent);
        ChatContentDTO answer = super.createAnswer(userSetting, question, answerContent);
        log.info("[{}] userId={}, paper={}, 取消论文, 对话入库", this.getPoint(), user.getId(), paper.getTitle());
        return answer;
    }

}


